package com.dongnao.jack.spring.parse;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

public class ReferenceBeanDefinitionParser implements BaseBeanDefinitionParseInterface {

    private String flag = "reference";

    @Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {

        if (support(element.getLocalName())) {
            RootBeanDefinition bf = new RootBeanDefinition(ReferenceConfig.class);
            bf.setLazyInit(false);
            String id = element.getAttribute("id");
            String interfaceClass = element.getAttribute("interface");
            String loadbalance = element.getAttribute("loadbalance");
            String protocol = element.getAttribute("protocol");
            bf.getPropertyValues().addPropertyValue("protocol", protocol);
            bf.getPropertyValues().addPropertyValue("loadbalance", loadbalance);
            bf.getPropertyValues().addPropertyValue("interfaceClass", interfaceClass);
            bf.getPropertyValues().addPropertyValue("id", id);

            parserContext.getRegistry().registerBeanDefinition(id, bf);
            return bf;
        }
        return null;
    }

    @Override
    public boolean support(String elementName) {

        return flag.equals(elementName);
    }

}
